package battlecruiser.ship.placement.ship;

import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author tymoshya
 * @since 14.06.12
 */
public enum ShipType {

    ONE_DECK(1),
    TWO_DECK(2),
    THREE_DECK(3),
    FOUR_DECK(4)
    ;

    private static Map<Integer, ShipType> stringShipTypeMap;
    private static Set<Integer> allowedValues;

    static {
        stringShipTypeMap = initStringShipTypeMap();
        allowedValues = initAllowedValues();
    }

    public static ShipType fromInteger(Integer s) {
        Assert.isTrue(allowedValues.contains(s), "Can create ShipType only from predefined constants!");
        return stringShipTypeMap.get(s);
    }

    private static Map<Integer, ShipType> initStringShipTypeMap() {
        Map<Integer, ShipType> result = new HashMap<Integer, ShipType>();
        result.put(1, ONE_DECK);
        result.put(2, TWO_DECK);
        result.put(3, THREE_DECK);
        result.put(4, FOUR_DECK);
        return result;
    }

    private static Set<Integer> initAllowedValues() {
        Set<Integer> result = new HashSet<Integer>();
        for (ShipType shipType : ShipType.values()) {
            result.add(shipType.getNumberOfDecks());
        }
        return result;
    }

    private int numberOfDecks;


    ShipType(int numberOfDecks) {
        this.numberOfDecks = numberOfDecks;
    }


    public int getNumberOfDecks() {
        return numberOfDecks;
    }
}
